﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Beeldbankmvc.Models
{
    public class VideoQueueRepository
    {
        BeeldbankLinqDataContext db = new BeeldbankLinqDataContext();

        public BeeldbankLinqDataContext GetDataContext()
        {
            return db;
        }

        public IQueryable<BEELDBANK_VIDEO_QUEUE> FindAllStatus(string status)
        {
            return from p in FindAll()
                   where p.Status.Equals(status)
                   orderby p.Id
                   select p;
        }

        public IQueryable<BEELDBANK_VIDEO_QUEUE> FindAll()
        {
            return db.BEELDBANK_VIDEO_QUEUEs;
        }

        public BEELDBANK_VIDEO_QUEUE FindOne(int id)
        {
            return db.BEELDBANK_VIDEO_QUEUEs.SingleOrDefault(m => m.Id == id);
        }

        public BEELDBANK_VIDEO_QUEUE FindOneByMediaObjectId(int mediaObjectId)
        {
            return db.BEELDBANK_VIDEO_QUEUEs.SingleOrDefault(m => m.MediaObjectId == mediaObjectId);
        }

        public void Add(int MediaObjectId, int FileNummer, String OriginalVideoPath)
        {
            BEELDBANK_VIDEO_QUEUE VideoQueue = new BEELDBANK_VIDEO_QUEUE();
            VideoQueue.MediaObjectId = MediaObjectId;
            VideoQueue.FileNummer = FileNummer;
            VideoQueue.OriginalVideoPath = OriginalVideoPath;
            VideoQueue.Status = "new";
            db.BEELDBANK_VIDEO_QUEUEs.InsertOnSubmit(VideoQueue);
            Save();
        }

        public void Add(BEELDBANK_VIDEO_QUEUE VideoQueue)
        {
            db.BEELDBANK_VIDEO_QUEUEs.InsertOnSubmit(VideoQueue);
        }

        public void UpdateStatus(int MediaObjectId, string newStatus)
        {
            BEELDBANK_VIDEO_QUEUE o = FindAll().Where(p => p.MediaObjectId == MediaObjectId).First();
            if (o != null)
            {
                o.Status = newStatus;
                Save();
            }
        }

        public void UpdateFileStatus(int MediaObjectId, string fileType)
        {
            BEELDBANK_VIDEO_QUEUE o = FindAll().Where(p => p.MediaObjectId == MediaObjectId).First();
            if (o != null)
            {
                switch (fileType)
                {
                    case "3gp":
                        o.Present_3gp = true;
                        break;
                    case "flv":
                        o.Present_flv = true;
                        break;
                    case "jpg":
                        o.Present_jpg = true;
                        break;
                    case "m4v":
                        o.Present_m4v = true;
                        break;
                    case "mp3":
                        o.Present_mp3 = true;
                        break;
                    case "mp4":
                        o.Present_mp4 = true;
                        break;
                    case "mpeg":
                        o.Present_mpeg = true;
                        break;
                    case "mpg":
                        o.Present_mpg = true;
                        break;
                    case "srt":
                        o.Present_srt = true;
                        break;
                    case "wmv":
                        o.Present_wmv = true;
                        break;
                }
                Save();
            }
        }
        public void Delete(BEELDBANK_VIDEO_QUEUE VideoQueue)
        {
            db.BEELDBANK_VIDEO_QUEUEs.DeleteOnSubmit(VideoQueue);
        }

        public void Save()
        {
            db.SubmitChanges();
        }
    }
}